package edu.calpoly.csc.coursebook.util.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;

import edu.calpoly.csc.coursebook.util.KeywordExtractor;
import edu.calpoly.csc.coursebook.util.RelevancyScorer;

public class CourseRelevancyScorer implements RelevancyScorer {
	
	public CourseRelevancyScorer() {
		
	}
	
	private static CourseRelevancyScorer _instance;
	public static CourseRelevancyScorer getInstance()
	{
	   if (null == _instance)
	   {
	      _instance = new CourseRelevancyScorer();
	   }
	   return _instance;
	}
	
	public int calculateScore(HashMap<String, Double> userKeywords, 
			HashMap<String, Double> courseUserMatches, List<String> origInterests) {
		double matches = 0.0;
		double score = 0.0;
		
//		String[] titleWords = courseTitle.split("\\s|[^\\w]|\\d");
//		for(String word : titleWords) {
//			for(String userKeyword : userKeywords) {
//				if(word.equalsIgnoreCase(userKeyword)) {
//					matches = 100;
//					break;
//				}
//			}
//			if(matches == 100)
//			{
//				score = (int) matches;
//				break;
//			}
//		}
		
		if(matches == 0 ) {
			/*
			for(String userKeyword : userKeywords) {
				for(String keyword : courseKeywords) {
					if (userKeyword.equalsIgnoreCase(keyword))
						matches++;
				}
			}
			*/
			
			
			Set<Entry<String, Double>> keywords = courseUserMatches.entrySet();
			for (Entry<String, Double> entry : keywords) {
	         	   score += (entry.getValue()/(userKeywords.get(entry.getKey())));
	        }
            
            matches = (score / origInterests.size()) * 100.0;
            
            if (matches > 100)
            	matches = 100;
            	
//			matches = ((double)(courseUserMatches.size())/(double)(userKeywords.size())) * (1000);
//			
//			if (matches > 100)
//				score = 100;
//			else if(matches < 1)
//				score = 10;
//			else
//				score = (int)matches;
		}
		return (int) matches;
	}

}
